import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
import 'package:tudouapp/util/_view_export.dart';

import '../util/_base_export.dart';

class AppTextService {
  static simpleText(String iv_text,
      {Color iv_color = Colors.blue, double iv_size = 15}) {
    return Text(
      iv_text,
      style: TextStyle(
        color: iv_color,
        fontSize: iv_size.sp,
      ),
    );
  }
}

class AppPopupService {
  static showText(String iv_text) {
    return BotToast.showText(
      text: iv_text,
      textStyle: TextStyle(color: Colors.red[400], fontSize: 16.sp),
      duration: Duration(seconds: AppPopup.bottomPopupDuration),
      onlyOne: true,
      clickClose: true,
      crossPage: true,
      align: Alignment(0, 1),
      animationDuration: Duration(milliseconds: 200),
      animationReverseDuration: Duration(milliseconds: 200),
      borderRadius: BorderRadius.circular(8.0),
      // backgroundColor: Colors.redAccent,
      contentColor: (Colors.red[100]!),
      // contentColor:Colors.blue,
      contentPadding: EdgeInsets.symmetric(
        vertical: 15.h,
        horizontal: 15.w,
      ),
    );
  }

  static showAlert(
    Icon iv_icon,
    String iv_title,
    String iv_content, {
    VoidCallback? cancel,
    VoidCallback? confirm,
  }) {
    return BotToast.showEnhancedWidget(
      backgroundColor: Color(0xbf808080),
      toastBuilder: (cancelFunc) => AlertDialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5)),
        title: Container(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[
                  iv_icon,
                  Text(
                    iv_title,
                    style: TextStyle(
                      fontWeight: FontWeight.w400,
                      fontSize: 20.sp,
                    ),
                  ),
                ],
              ),
              Padding(
                padding: EdgeInsets.symmetric(vertical: 10.h),
                child: Divider(
                  height: 2,
                  color: Colors.grey,
                  indent: 0,
                  endIndent: 60,
                ),
              ),
              Text(iv_content),
            ],
          ),
        ),
        actions: <Widget>[
          if (cancel != null)
            TextButton(
              onPressed: () {
                cancelFunc();
                cancel.call();
              },
              style: ButtonStyle(
                overlayColor: MaterialStateColor.resolveWith((states) => Color(0x55FF8A80)),
                //shadowColor: Color(0x99FF8A80),
              ),
              // highlightColor: const Color(0x55FF8A80),
              // splashColor: const Color(0x99FF8A80),
              child: Text(
                'cancel',
                style: TextStyle(color: Colors.redAccent, fontSize: 20.sp),
              ),
            ),
          TextButton(
            onPressed: () {
              cancelFunc();
              confirm?.call();
            },
            child: Text(
              'confirm',
              style: TextStyle(color: Colors.blue, fontSize: 20.sp),
            ),
          ),
        ],
      ),
      // duration: Duration(milliseconds: 300)
    );
  }

  static showAlertSimple(
    String iv_icon_type,
    String iv_title,
    String iv_content, {
    String iv_confirm_text = '好的',
    VoidCallback? confirm,
    String iv_cancel_text = '取消',
    VoidCallback? cancel,
    Duration? iv_duration,
  }) {
    IconData? lv_icon_data;
    Color? lv_icon_color;

    switch (iv_icon_type) {
      case 'E':
        // lv_icon_data = MaterialCommunityIcons.emoticon_sad_outline;
        lv_icon_data = LineIcons.cryingFace;
        lv_icon_color = Color(0xffFF3434);
        break;
      case 'S':
        // lv_icon_data = MaterialCommunityIcons.emoticon_happy_outline;
        lv_icon_data = LineIcons.smilingFace;
        lv_icon_color = Color(0xff30C4B3);
        break;
      case 'I':
        // lv_icon_data = MaterialCommunityIcons.emoticon_cool_outline;
        lv_icon_data = LineIcons.winkingFace;
        lv_icon_color = Color(0xff149AFC);
        break;
      case 'W':
        // lv_icon_data = MaterialCommunityIcons.emoticon_neutral_outline;
        lv_icon_data = LineIcons.flushedFace;
        lv_icon_color = Color(0xffFF9F05);
        break;
      default:
    }
    return BotToast.showEnhancedWidget(
      duration: iv_duration,
      allowClick: false,
      backgroundColor: Color(0xbf808080),
      toastBuilder: (cancelFunc) => AlertDialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5)),
        title: Container(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                child: Icon(lv_icon_data, size: 60, color: lv_icon_color),
              ),
              Container(
                child: Text(
                  iv_title,
                  style: TextStyle(
                    color: lv_icon_color,
                    fontWeight: FontWeight.w400,
                    fontSize: 20.sp,
                  ),
                ),
              ),
              SizedBox(
                height: 20.h,
              ),
              Container(
                child: Text(
                  iv_content,
                  softWrap: true,
                  maxLines: 5,
                  style: TextStyle(
                    color: AppColor.blackMain,
                    fontSize: 14.sp,
                  ),
                ),
              ),
            ],
          ),
        ),
        actions: <Widget>[
          if (cancel != null)
            TextButton(
              onPressed: () {
                cancelFunc();
                cancel.call();
              },
              child: Text(
                iv_cancel_text,
                style: TextStyle(color: Colors.blue, fontSize: 15.sp),
              ),
            ),
          TextButton(
            onPressed: () {
              cancelFunc();
              confirm?.call();
            },
            child: Text(
              iv_confirm_text,
              style: TextStyle(color: Colors.blue, fontSize: 15.sp),
            ),
          ),
        ],
      ),
      // duration: Duration(milliseconds: 300)
    );
  }
}

class AppPopup {
  static int bottomPopupDuration = 3;
  static void nullFunction() {}
  static getConfig(String iv_type) {
    var lm_popup_config = {};

    lm_popup_config['content_color'] = Color(0xff606880);
    lm_popup_config['text_color'] = Color(0xffffffff);
    switch (iv_type) {
      case 'E':
        // lm_popup_config['icon'] = MaterialCommunityIcons.window_close;
        lm_popup_config['icon'] = LineIcons.windowClose;
        lm_popup_config['type_color'] = Color(0xffFD5D53);
        lm_popup_config['text_color'] = Color(0xffFF3434);

        break;
      case 'S':
        // lm_popup_config['icon'] = MaterialCommunityIcons.check;
        lm_popup_config['icon'] = Icons.check;

        lm_popup_config['type_color'] = Color(0xff4CD964);
        lm_popup_config['text_color'] = Color(0xff4CD964);
        break;
      case 'I':
        // lm_popup_config['icon'] = MaterialCommunityIcons.alert;
        lm_popup_config['icon'] = Icons.add_alert;
        lm_popup_config['type_color'] = Color(0xff4F7BFC);
        lm_popup_config['text_color'] = Color(0xff4F7BFC);
        break;
      case 'W':
        // lm_popup_config['icon'] = MaterialCommunityIcons.emoticon_neutral_outline;
        lm_popup_config['icon'] = LineIcons.flushedFace;
        lm_popup_config['type_color'] = Color(0xffFEC950);
        lm_popup_config['text_color'] = Color(0xffFFBE2B);
        break;
      default:
    }
    return lm_popup_config;
  }

  static popupToast(String iv_type, String iv_text) {
    var lm_popup_config = getConfig(iv_type);
    return BotToast.showText(
      text: iv_text,
      textStyle: TextStyle(
        color: Colors.white,
        fontSize: 18,
      ),
      duration: Duration(seconds: 3), //AppPopup.bottomPopupDuration),
      onlyOne: true,
      clickClose: true,
      crossPage: true,
      align: Alignment(0, 1),
      animationDuration: Duration(milliseconds: 200),
      animationReverseDuration: Duration(milliseconds: 200),
      borderRadius: BorderRadius.circular(8.0),
      // backgroundColor: Colors.redAccent,
      contentColor: lm_popup_config['text_color'],
      contentPadding: EdgeInsets.symmetric(
        vertical: 15,
        horizontal: 15,
      ),
    );
  }

  static popUpInfo({
    bool allowClick = true,
    String iv_confirm_text = 'OK',
    VoidCallback? confirm,
    Duration? iv_duration,
  }) {
    return BotToast.showEnhancedWidget(
      duration: iv_duration,
      allowClick: allowClick,
      backgroundColor: Color(0xbf808080),
      toastBuilder: (cancelFunc) => AlertDialog(
        titlePadding: EdgeInsets.all(0),
        contentPadding: EdgeInsets.all(0),
        buttonPadding: EdgeInsets.symmetric(horizontal: 10),
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
        content: Container(
          height: 342.h,
          padding: EdgeInsets.symmetric(horizontal: 45.w, vertical: 18.h),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              SizedBox(height: 22.h),
              Container(
                  child: Center(
                child: Text(
                  '追单使用说明',
                  softWrap: true,
                  // maxLines: 5,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 16.sp,
                  ),
                ),
              )),
              SizedBox(height: 27.h),
              Container(
                child: Text(
                  '● 当上个订单还未进入打包阶段时，您可以选择追加订单。',
                  softWrap: true,
                  // maxLines: 5,
                  style: TextStyle(
                    color: Color(0xff4E4E4E),
                    fontSize: 12.sp,
                  ),
                ),
              ),
              SizedBox(height: 22.h),
              Container(
                child: Text(
                  '● 追加的订单将自动与旧订单合并，无需凑单免运或再付运费。 ',
                  softWrap: true,
                  // maxLines: 5,
                  style: TextStyle(
                    color: Color(0xff4E4E4E),
                    fontSize: 12.sp,
                  ),
                ),
              ),
              SizedBox(height: 22.h),
              Container(
                child: Text(
                  '● 新订单的收货人信息和配送时间需要与旧订单统一。',
                  softWrap: true,
                  // maxLines: 5,
                  style: TextStyle(
                    color: Color(0xff4E4E4E),
                    fontSize: 12.sp,
                  ),
                ),
              ),
              SizedBox(height: 22.h),
              Container(
                child: Text(
                  '● 追加的订单不得与旧订单的优惠活动叠加。',
                  softWrap: true,
                  // maxLines: 5,
                  style: TextStyle(
                    color: Color(0xff4E4E4E),
                    fontSize: 12.sp,
                  ),
                ),
              ),
            ],
          ),
        ),
        actions: <Widget>[
          FlatButton(
            onPressed: () {
              cancelFunc();
              confirm?.call();
            },
            child: Text(
              iv_confirm_text,
              style: TextStyle(color: Color(0xffFF9F05), fontSize: 15),
            ),
          ),
        ],
      ),
      // duration: Duration(milliseconds: 300)
    );
  }

  static popUpCancel({
    bool allowClick = true,
    String iv_confirm_text = 'OK',
    VoidCallback? confirm,
    Duration? iv_duration,
  }) {
    return BotToast.showEnhancedWidget(
      duration: iv_duration,
      allowClick: allowClick,
      backgroundColor: Color(0xbf808080),
      toastBuilder: (cancelFunc) => AlertDialog(
        titlePadding: EdgeInsets.all(0),
        contentPadding: EdgeInsets.all(0),
        buttonPadding: EdgeInsets.symmetric(horizontal: 10),
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
        content: Container(
          height: 200.h,
          padding: EdgeInsets.symmetric(horizontal: 45.w, vertical: 18.h),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              SizedBox(height: 22.h),
              Container(
                  child: Center(
                child: Text(
                  '取消追加订单',
                  softWrap: true,
                  // maxLines: 5,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 16.sp,
                  ),
                ),
              )),
              SizedBox(height: 27.h),
              Container(
                child: Text(
                  '● 改变配送时间时, 会自动为您取消追加订单。',
                  softWrap: true,
                  // maxLines: 5,
                  style: TextStyle(
                    color: Color(0xff4E4E4E),
                    fontSize: 12.sp,
                  ),
                ),
              ),
              SizedBox(height: 22.h),
              Container(
                child: Text(
                  '● 改变配送地址时, 会自动为您取消追加订单。 ',
                  softWrap: true,
                  // maxLines: 5,
                  style: TextStyle(
                    color: Color(0xff4E4E4E),
                    fontSize: 12.sp,
                  ),
                ),
              ),
            ],
          ),
        ),
        actions: <Widget>[
          FlatButton(
            onPressed: () {
              cancelFunc();
              confirm?.call();
            },
            child: Text(
              iv_confirm_text,
              style: TextStyle(color: Color(0xffFF9F05), fontSize: 15),
            ),
          ),
        ],
      ),
      // duration: Duration(milliseconds: 300)
    );
  }

  static popupAlert(
    String iv_type,
    String iv_title,
    String iv_content, {
    bool allowClick = true,
    String iv_confirm_text = 'Okay',
    VoidCallback? confirm,
    String iv_cancel_text = '',
    VoidCallback? cancel,
    Duration? iv_duration,
  }) {
    var lm_popup_config = getConfig(iv_type);

    return BotToast.showEnhancedWidget(
      duration: iv_duration,
      allowClick: allowClick,
      backgroundColor: Color(0xbf808080),
      toastBuilder: (cancelFunc) => AlertDialog(
        titlePadding: EdgeInsets.all(0),
        contentPadding: EdgeInsets.all(0),
        buttonPadding: EdgeInsets.symmetric(horizontal: 10),
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5)),
        title: Container(
          height: 100,
          width: ScreenUtil().screenWidth,
          color: lm_popup_config['type_color'],
          child: Icon(
            lm_popup_config['icon'],
            size: 60,
            color: Colors.white,
          ),
        ),
        content: Container(
          height: 260,
          padding: EdgeInsets.symmetric(horizontal: 10, vertical: 20),
          child: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Container(
                  child: Text(
                    iv_title,
                    style: TextStyle(
                      color: lm_popup_config['type_color'],
                      fontWeight: FontWeight.w300,
                      fontSize: 20.sp,
                    ),
                  ),
                ),
                SizedBox(
                  height: 20.h,
                ),
                Container(
                  child: Text(
                    iv_content,
                    softWrap: true,
                    // maxLines: 5,
                    style: TextStyle(
                      color: AppColor.blackMain,
                      fontSize: 14.sp,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
        actions: <Widget>[
          if (cancel != null)
            TextButton(
              onPressed: () {
                cancelFunc();
                cancel.call();
              },
              child: Text(
                iv_cancel_text,
                style: TextStyle(color: AppColor.greyText, fontSize: 15),
              ),
            ),
          TextButton(
            onPressed: () {
              cancelFunc();
              confirm?.call();
            },
            child: Text(
              iv_confirm_text,
              style: TextStyle(color: AppColor.blueMain, fontSize: 15),
            ),
          ),
        ],
      ),
      // duration: Duration(milliseconds: 300)
    );
  }
}
